package com.ytjj.qmyx.mall.controller.h5;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.SysConfigConstants;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.SysConfigUtil;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.enums.ActivityTypeEnums;
import com.ytjj.qmyx.mall.mapper.CouponRecordsDao;
import com.ytjj.qmyx.mall.model.CouponRecords;
import com.ytjj.qmyx.mall.model.response.ActivityProductResponse;
import com.ytjj.qmyx.mall.model.response.H5GetScoreIndexResponse;
import com.ytjj.qmyx.mall.model.response.ProductResponse;
import com.ytjj.qmyx.mall.service.ActivityService;
import com.ytjj.qmyx.mall.service.h5.H5CashCouponService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 红包抵扣
 *
 **/
@Slf4j
@RestController
@RequestMapping("/h5/cashcoupon")
public class H5CashCouponController extends BaseController {
    @Resource
    private H5CashCouponService h5CashCouponService;

    @Resource
    private CouponRecordsDao couponRecordsDao;

    @Resource
    private ActivityService activityService;

    @Resource
    private RedisService redisService;

    @Autowired
    private SysConfigUtil sysConfigUtil;

    @GetMapping("/getCouponList")
    public CommonResult getCouponList(@RequestParam(value = "columnId", required = false, defaultValue = "0") Integer columnId,
                                       @RequestParam(value = "pageSize", required = false, defaultValue = "30") Integer pageSize,
                                       @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag) {
        List<ProductResponse> couponList;
        String key = com.ytjj.qmyx.mall.constants.RedisConstants.ACTIVITY_PRODUCT + ActivityTypeEnums.CASH_COUPON.name() + TokenUtil.getPlatformData();
        String result = redisService.get(key);
        if (StringUtils.isNotEmpty(result)) {
            try {
                couponList = JSONArray.parseArray(result, ProductResponse.class);
            } catch (Exception e) {
                log.info("优惠券序列化异常：", e);
                couponList = h5CashCouponService.getCouponList(pageNum,pageSize,columnId,flag);
                redisService.set(key, JSON.toJSONString(couponList));
                redisService.expire(key, Integer.valueOf(sysConfigUtil.getSysConfig(SysConfigConstants.REDIS_TIME)));
            }
        } else {
            couponList = h5CashCouponService.getCouponList(pageNum,pageSize,columnId,flag);
            redisService.set(key, JSON.toJSONString(couponList));
            redisService.expire(key, Integer.valueOf(sysConfigUtil.getSysConfig(SysConfigConstants.REDIS_TIME)));
        }
        // 是否领取
        Date nowDate = new Date();
        String token;
        String source = TokenUtil.getSource();
        if ("h5".equals(source)) {
            token = TokenUtil.getToken();
        } else {
            token = TokenUtil.getUuid();
        }
        couponList = couponList.stream()
                .map(item -> {
                    CouponRecords couponRecords = couponRecordsDao.queryOne(item.getId(), token, nowDate, source);
                    if (couponRecords != null) {
                        item.setIsReceive(2);
                        item.setOverdueTime(couponRecords.getOverdueTime());
                    }
                    return item;
                })
                .collect(Collectors.toList());

        return CommonResult.success(couponList);
    }

    @GetMapping("/getUserCouponList")
    public CommonResult getUserCouponList(@RequestParam(value = "obtain", required = false, defaultValue = "0") Integer obtain) {
        return CommonResult.success(h5CashCouponService.getUserCouponList(obtain));
    }

    @GetMapping("/obtainCoupon")
    public CommonResult obtainCoupon(@RequestParam(value = "productId", required = true) Integer productId) {
        CouponRecords couponRecord = h5CashCouponService.obtainCoupon(productId);
        return CommonResult.success(couponRecord);
    }

    /**
     * 栏目商品列表
     *
     * @return
     */
    @GetMapping("/queryListByPage")
    public CommonResult queryListByPage(@RequestParam(value = "columnId", required = false, defaultValue = "0") Integer columnId,
                                        @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                        @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag,
                                        @RequestParam(value = "jptype", required = false, defaultValue = "0") Integer jptype) {
        if(1==jptype){
            columnId=-1;
        }
        List<ActivityProductResponse> productList = activityService.getActivityList(pageNum, pageSize,ActivityTypeEnums.CASH_COUPON.name(),columnId);
        if(1==flag){//打乱顺序
            Collections.shuffle(productList);
        }
        return CommonResult.success(productList);
    }
}
